﻿using HXDNB.DAL;
using HXDNB.Infrastructure;
using System;
using System.Linq;
using System.Web.Mvc;

namespace HXDNB.CMS.Controllers
{
    public class UserController : BaseController
    {

        //用户列表
        public ActionResult UserList(FormCollection Form, int index = 0)
        {
            UserBLL Bll = new UserBLL();
            IQueryable<UserInfo> List = Bll.GetList(Form["Name"]);
            Paging<UserInfo> Pag = new Paging<UserInfo>(index, List);
            ViewBag.PagList = Pag;
            return View(Form);
        }

        //添加/修改用户        
        public ActionResult Info(int? ID)
        {
            UserBLL Bll = new UserBLL();
            UserInfo info = new UserInfo();
            if (ID != null)
            {
                info = Bll.Get((int)ID);
            }
            return View(info);
        }
        //添加/修改用户
        [HttpPost]
        public ActionResult Info(UserInfo User)
        {
            UserBLL Bll = new UserBLL();
            if (string.IsNullOrWhiteSpace(Request.QueryString["id"]))//添加用户
            {
                if (Bll.GetList(User.Name).Any())//判断用户是否存在
                {
                    ViewBag.result = -1;
                }
                else
                {
                    ViewBag.result = Bll.Add(User) > 0 ? 1 : 0;
                }
            }
            else//修改用户角色类型
            {
                User.MeunRole = "," + Request.Form["MeunRole"] + ",";
                ViewBag.result = Bll.EditRole(User) ? 2 : 0;
            }
            return View(User);
        }
        //修改密码
        public ActionResult Edit()
        {
            return View();
        }
        //修改密码
        [HttpPost]
        public ActionResult Edit(UserInfo info)
        {
            if (Request.Form["Pwd"].Trim() == Request.Form["Pwd2"].Trim())
            {
                UserBLL BLL = new UserBLL();
                info.Id = Convert.ToInt32(Session["UserId"]);
                ViewBag.result = BLL.EditPwd(info) ? 1 : 0;
            }
            else
            {
                ViewBag.result = -1;
            }
            return View();
        }
        //删除用户
        [HttpPost]
        public string Delete(string id = "")
        {
            var bll = new UserBLL();
            bool result = true;
            foreach (string item in id.Split(',').Where(item => !string.IsNullOrWhiteSpace(item)))
            {
                result = bll.Delete(int.Parse(item));
                if (result == false)
                {
                    break;
                }
            }
            return result.ToString();
        }
        //重置用户密码
        [HttpPost]
        public string Reset(int id)
        {
            var bll = new UserBLL();
            bool result = bll.Reset(id);
            return result.ToString();
        }
        //编辑角色权限(默认角色为管理员)
        public ActionResult EditRoles(string roleid = "1")
        {
            ViewBag.roleid = roleid;
            return View();
        }
        //编辑角色权限
        [HttpPost]
        public ActionResult EditRoles(FormCollection form)
        {
            string roleid = form["roleid"];
            string pageValue = form["pageValue"];
            UserBLL bll = new UserBLL();
            ViewBag.roleid = roleid;
            ViewBag.result = bll.EditPageRoles(roleid, pageValue).ToString();
            return View();
        }
    }
}
